require(["config"],function(){
	require(["jquery","template","zoom","fly","cookie","header","footer"],function($,template){
		$.getJSON("http://rap2api.taobao.org/app/mock/25375/detail",function(data){
			console.log(data)
			const html = template("list_temp",{list:data.res_body});
			$(".center").html(html);

		
		}).done(function(){
			$("#imgA").elevateZoom({gallery:'gal1',cursor:'pointer',galleryActiveClass:'active'});
		}).done(function(){
			$(".buy_amount").on("click",".btn_jia,.btn_jian",function(){
				let a = Number($("#buyNum").val())
				if($(this).is(".btn_jia")){
					$("#buyNum").val(a+1);
				}
				if($(this).is(".btn_jian")){
					if(a<=1){
						a=1;
						return;
					}
					$("#buyNum").val(a-1);
				}
				
			})
			$(".add_to_carts").click(function(e){
				const end = $(".myCarts").offset();
				const start = {left:e.pageX, top:e.pageY};
				const flyer = $(`<img src="/images/111.jpg" style="width: 20px;height: 20px;">`)
				flyer.fly({
					start:{
						left : start.left,
						top : start.top - $(window).scrollTop()
					},
					end:{
						left : end.left,
						top : end.top - $(window).scrollTop()
					},
					onEnd :function(){
						this.destroy();
					}
				});

				const currProd = {
					id:$(".id").text(),
					img:$(".img").attr("src"),
					desc:$(".desc").text(),
					price:$(".vip_price").text(),
					title:$("h2").text(),
					amount:Number($("#buyNum").val())
				}
				$.cookie.json = true;
				const products = $.cookie("products") || [];
				const has = products.some(function(curr){
					if(curr.id === currProd.id){
						curr.amount++;
						return true;
					}
				})
				if(!has)
					products.push(currProd);
				$.cookie("products",products,{expires:10,path:"/"});
				$(".cart").text(Number($(".cart").text())+currProd.amount);
				$(".mycart").text(Number($(".cart").text())+currProd.amount);

			});
		})
	});
});